package se2;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Time;
import java.util.ArrayList;
import java.util.List;

public class DiscountPlan implements Comparable
{
	private int		id;
	private String	name;
	private String	startDay;
	private Time	startTime;
	private String	endDay;
	private Time	endTime;
	private double	rate;

	public DiscountPlan()
	{
	}

	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getStartDay()
	{
		return startDay;
	}

	public void setStartDay(String startDay)
	{
		this.startDay = startDay;
	}

	public Time getStartTime()
	{
		return startTime;
	}

	public void setStartTime(Time startTime)
	{
		this.startTime = startTime;
	}

	public String getEndDay()
	{
		return endDay;
	}

	public void setEndDay(String endDay)
	{
		this.endDay = endDay;
	}

	public Time getEndTime()
	{
		return endTime;
	}

	public void setEndTime(Time endTime)
	{
		this.endTime = endTime;
	}

	public double getRate()
	{
		return rate;
	}

	public void setRate(double rate)
	{
		this.rate = rate;
	}

	public String toString()
	{
		return name + " | " + rate + " | " + startTime + " | " + endTime;
	}

	private Connection getConnection() throws SQLException, ClassNotFoundException
	{
		Class.forName("org.sqlite.JDBC");
		String home = System.getProperty("user.home");
		Connection conn = DriverManager.getConnection("jdbc:sqlite:" + home + "/db/VoIP");

		return conn;
	}

	public void loadAllInformation()
	{
		try
		{
			PreparedStatement ps;
			ResultSet rs;

			ps = getConnection().prepareStatement("select * from DiscountPlan where id = ?");
			ps.setInt(1, this.id);
			rs = ps.executeQuery();

			if (rs.next())
			{
				this.id = rs.getInt(1);
				this.name = rs.getString(2);
				this.startDay = rs.getString(3);
				this.startTime = rs.getTime(4);
				this.endDay = rs.getString(5);
				this.endTime = rs.getTime(6);
				this.rate = rs.getDouble(7);
			}
			rs.close();
		}
		catch (Exception e)
		{
			System.out.println("Error loading DiscountPlans (DiscountPlan.java, loadAllInformation()).");
			e.printStackTrace();
		}
	}

	public List<DiscountPlan> getAllDiscountPlans()
	{
		try
		{
			PreparedStatement ps;
			ResultSet rs;

			ps = getConnection().prepareStatement("select * from DiscountPlan");
			rs = ps.executeQuery();

			List<DiscountPlan> discountPlans = new ArrayList<DiscountPlan>();
			while (rs.next())
			{
				DiscountPlan dp = new DiscountPlan();

				dp.setId(rs.getInt(1));
				dp.setName(rs.getString(2));
				dp.setStartDay(rs.getString(3));
				dp.setStartTime(rs.getTime(4));
				dp.setEndDay(rs.getString(5));
				dp.setEndTime(rs.getTime(6));
				dp.setRate(rs.getDouble(7));

				discountPlans.add(dp);
			}
			rs.close();

			return discountPlans;
		}
		catch (Exception e)
		{
			System.out.println("Error loading DiscountPlans (DiscountPlan.java, getAllDiscountPlans()).");
			e.printStackTrace();
			return null;
		}
	}

	public boolean addDiscountPlan()
	{
		try
		{
			Connection conn = getConnection();

			PreparedStatement ps;
			ResultSet rs;

			// Check for duplicates by name
			ps = conn.prepareStatement("select * from DiscountPlan where name = ?");
			ps.setString(1, this.name);
			rs = ps.executeQuery();

			// If the ResulSet is populated, there is already a BillingPlan with the same name, fail.
			if (rs.next())
			{
				System.out.println("DiscountPlan with given name already exists.");
				return false;
			}
			rs.close();

			// Insert into database and get the auto-generated id
			ps = conn
					.prepareStatement("insert into DiscountPlan (name, startDay, startTime, endDay, endTime, rate) values (?, ?, ?, ?, ?, ?)");
			ps.setString(1, this.name);
			ps.setString(2, this.startDay);
			ps.setTime(3, this.startTime);
			ps.setString(4, this.endDay);
			ps.setTime(5, this.endTime);
			ps.setDouble(6, this.rate);
			ps.execute();

			// Query the database for the DiscountPlan's unique id
			ps = conn.prepareStatement("select id from DiscountPlan where name = ?");
			ps.setString(1, this.name);
			rs = ps.executeQuery();

			int newId = 0;
			if (rs.next())
			{
				newId = rs.getInt(1);
				id = newId;
			}
			rs.close();

			conn.close();

			return true;
		}
		catch (Exception e)
		{
			System.out
					.println("Failed in adding a DiscountPlan to the database (DiscountPlan.java, addDiscountPlan()).");
			e.printStackTrace();
			return false;
		}
	}

	public boolean editDiscountPlan()
	{
		try
		{
			Connection conn = getConnection();

			PreparedStatement ps;

			ps = conn
					.prepareStatement("update BillingPlan set name=?, startDay=?, startTime=?, endDay=?, endTime=?, rate=? where id=?");
			ps.setString(1, this.name);
			ps.setString(2, this.startDay);
			ps.setTime(3, this.startTime);
			ps.setString(4, this.endDay);
			ps.setTime(5, this.endTime);
			ps.setDouble(6, this.rate);
			ps.setInt(7, this.id);
			ps.execute();

			conn.close();

			return true;

		}
		catch (Exception e)
		{
			System.out
					.println("Failed in editing a DiscountPlan to the database (DiscountPlan.java, editDiscountPlan()).");
			e.printStackTrace();
			return false;
		}
	}

	public boolean removeDiscountPlan(int id)
	{
		try
		{
			Connection conn = getConnection();

			PreparedStatement ps;

			ps = conn.prepareStatement("delete from BillingToDiscount where disc_id = ?");
			ps.setInt(1, id);
			ps.execute();

			ps = conn.prepareStatement("delete from DiscountPlan where id = ?");
			ps.setInt(1, id);
			ps.execute();

			conn.close();

			return true;
		}
		catch (Exception e)
		{
			System.out
					.println("Failed in removing a DiscountPlan to the database (DiscountPlan.java, removeDiscountPlan()).");
			e.printStackTrace();
			return false;
		}
	}

	public boolean equals(Object o)
	{
		DiscountPlan dp = (DiscountPlan) o;

		if (this.id == dp.getId())
		{
			return true;
		}
		return false;
	}

	public int compareTo(Object o)
	{
		DiscountPlan dp = (DiscountPlan) o;

		if (this.id < dp.getId())
		{
			return -1;
		}
		else if (this.id > dp.getId())
		{
			return 1;
		}
		return 0;
	}

}
